//
//  CustomTabBar.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/18.
//

#import "CustomTabBar.h"

@interface CustomTabBar()<CustomTabButtonDelegate>

@property (nonatomic, weak) CustomTabButton *selectedButton;

@property(nonatomic, strong) NSMutableArray *tabbarBtnArray;

@property (nonatomic, strong) NSMutableArray *norImageArrM;

@property (nonatomic, strong) NSMutableArray *selImageArrM;

@property (nonatomic, strong) UIImageView *bgImageView;

@property (nonatomic) CGFloat customTabBarWidth;

@end

@implementation CustomTabBar

- (NSMutableArray *)tabbarBtnArray {
    if (!_tabbarBtnArray) {
        _tabbarBtnArray = [NSMutableArray array];
    }
    return  _tabbarBtnArray;
}

- (NSMutableArray *)norImageArrM {
    if (!_norImageArrM) {
        _norImageArrM = [NSMutableArray array];
    }
    return _norImageArrM;
}

- (NSMutableArray *)selImageArrM {
    if (!_selImageArrM) {
        _selImageArrM = [NSMutableArray array];
    }
    return _selImageArrM;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]init];
        _bgImageView.userInteractionEnabled = YES;
        _bgImageView.backgroundColor = kButtonDefaultColor;
        _bgImageView.layer.cornerRadius = kTabBarCustomeHeight/2;
        _bgImageView.layer.masksToBounds = YES;
    }
    return _bgImageView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
       if (self) {
           
           self.barTintColor = kTableViewBackColor;
           
           [self addSubview:self.bgImageView];
           
           self.customTabBarWidth = DimensionBaseIphone6(200);
           if ([CurrentUser mine].isYNLanguage) {
               self.customTabBarWidth = DimensionBaseIphone6(147);
           } 
           
           self.bgImageView.frame = CGRectMake((kScreenWidth - self.customTabBarWidth)/2, 0, self.customTabBarWidth, kTabBarCustomeHeight);
                   
       }
    return self;

}


- (void)addTabBarButtonNorImageUrl:(NSString *)norImageUrl selImageUrl:(NSString *)selImageUrl withSel:(NSInteger)index{

    CustomTabButton *tabBarBtn = [[CustomTabButton alloc] init];
    tabBarBtn.delegate = self;
    [self addSubview:tabBarBtn];

    [tabBarBtn setTabBarImageUrl:norImageUrl];

    [tabBarBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];

    [self.tabbarBtnArray addObject:tabBarBtn];
    [self.norImageArrM addObject:norImageUrl];
    [self.selImageArrM addObject:selImageUrl];
    
    if (self.tabbarBtnArray.count == index) {
        [self buttonClick:tabBarBtn];
        [tabBarBtn.iconBtn setSelected:YES];
    }
    

}

- (void)handleSelectButton:(CustomTabButton *)cusBtn{
    for (int i = 0; i < self.tabbarBtnArray.count; i++) {
        CustomTabButton *currentButton = self.tabbarBtnArray[i];
        if (currentButton == cusBtn) {
            [currentButton.iconBtn setSelected:YES];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateSelected];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateNormal];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.selImageArrM[i]] forState:UIControlStateHighlighted];
        } else {
            [currentButton.iconBtn setSelected:NO];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateSelected];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateNormal];
            [currentButton.iconBtn setBackgroundImage:[UIImage imageNamed:self.norImageArrM[i]] forState:UIControlStateHighlighted];
        }
    }
}

#pragma mark -  <MyTabBarButtonDelegate>
- (void)customTabButtonClick:(CustomTabButton *)myButton{
    [self buttonClick:myButton];
}

- (void)buttonClick:(CustomTabButton *)button {
    
    if (button.tag != 0) {
        
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
            [[DKCore sharedDKCore] initLocation];
            return;
        }
        [[DKCore sharedDKCore] initLocation];
        
    }
    
    if (_clickBtnBlock) {
        _clickBtnBlock(button.tag);
    }
    
    if (![[CurrentUser mine] hasLogged] && button.tag != 0) {
        return;
    }
    
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    
    [self handleSelectButton:self.selectedButton];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = kTabBarCustomeHeight + kDefaultTabBarFixHeight;
    CGRect newFrame = self.frame;
    newFrame.size.height = height;
    self.frame = newFrame;

    CGFloat buttonH =  DimensionBaseIphone6(40);
    CGFloat buttonW = buttonH;
        
    CGFloat buttonY = (kTabBarCustomeHeight - buttonH)/2;
    
    CGFloat fix = (self.customTabBarWidth - buttonW *self.tabbarBtnArray.count - DimensionBaseIphone6(10))/(self.tabbarBtnArray.count - 1);
    
    for (int index = 0; index < self.tabbarBtnArray.count; index++) {

        CustomTabButton *button = self.tabbarBtnArray[index];

        CGFloat buttonX = (kScreenWidth - self.customTabBarWidth)/2 + index * buttonW + DimensionBaseIphone6(5) + fix*index;

        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);

        button.tag = index;
    }
    
    
}

@end
